<?php

/**
 * This is the model class for table "xman_project".
 *
 * The followings are the available columns in table 'xman_project':
 * @property integer $project_id
 * @property string $project_code
 * @property string $project_name
 * @property string $project_description
 * @property string $project_logo
 * @property string $project_status
 * @property double $project_budget
 * @property integer $project_category_id
 * @property string $project_partner
 * @property integer $project_programming_language_id
 * @property integer $project_manager_id
 * @property integer $project_labor
 * @property double $project_cost
 * @property string $project_currency
 * @property integer $project_progress
 * @property string $project_rating
 * @property double $project_profit
 * @property string $project_created_date
 * @property integer $project_created_by_user_id
 * @property string $project_start_date
 * @property string $project_end_date
 * @property string $project_last_activity_date
 *
 * The followings are the available model relations:
 * @property Billing[] $billings
 * @property Bug[] $bugs
 * @property Comment[] $comments
 * @property MessageCategory[] $messageCategories
 * @property NotebookCategory[] $notebookCategories
 * @property ProjectCategory $projectCategory
 * @property User $projectCreatedByUser
 * @property User $projectManager
 * @property ProgrammingLanguage $projectProgrammingLanguage
 * @property ProjectRole[] $projectRoles
 * @property ProjectUser[] $projectUsers
 * @property Risk[] $risks
 * @property Schedule[] $schedules
 * @property TaskList[] $taskLists
 * @property UserRate[] $userRates
 */
class Project extends CActiveRecord
{
	 public $image;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Project the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_project';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('project_code, project_name, project_category_id, project_currency, project_created_by_user_id', 'required'),
			array('project_category_id, project_programming_language_id, project_manager_id, project_labor, project_progress, project_created_by_user_id', 'numerical', 'integerOnly'=>true),
			array('project_budget, project_cost, project_profit', 'numerical'),
			array('project_code, project_name,project_partner, project_currency, project_rating', 'length', 'max'=>50),
			array('project_logo, project_status,project_description,','length','max'=>500),
			array('project_created_date, project_start_date, project_end_date, project_last_activity_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('project_id, project_code, project_name, project_description, project_logo, project_status, project_budget, project_category_id, project_partner, project_programming_language_id, project_manager_id, project_labor, project_cost, project_currency, project_progress, project_rating, project_profit, project_created_date, project_created_by_user_id, project_start_date, project_end_date, project_last_activity_date', 'safe', 'on'=>'search'),
			array('image','file','types'=>'jpg,png','allowEmpty' => true),
			
			);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'billings' => array(self::HAS_MANY, 'Billing', 'billing_project_id'),
			'bugs' => array(self::HAS_MANY, 'Bug', 'bug_project_id'),
			'comments' => array(self::HAS_MANY, 'Comment', 'comment_project_id'),
			'messageCategories' => array(self::HAS_MANY, 'MessageCategory', 'message_category_project_id'),
			'notebookCategories' => array(self::HAS_MANY, 'NotebookCategory', 'notebook_category_project_id'),
			'projectCategory' => array(self::BELONGS_TO, 'ProjectCategory', 'project_category_id'),
			'projectCreatedByUser' => array(self::BELONGS_TO, 'User', 'project_created_by_user_id'),
			'projectManager' => array(self::BELONGS_TO, 'User', 'project_manager_id'),
			'projectProgrammingLanguage' => array(self::BELONGS_TO, 'ProgrammingLanguage', 'project_programming_language_id'),
			'projectRoles' => array(self::HAS_MANY, 'ProjectRole', 'project_role_project_id'),
			'projectUsers' => array(self::HAS_MANY, 'ProjectUser', 'project_user_project_id'),
			'risks' => array(self::HAS_MANY, 'Risk', 'risk_project_id'),
			'schedules' => array(self::HAS_MANY, 'Schedule', 'schedule_project_id'),
			'taskLists' => array(self::HAS_MANY, 'TaskList', 'tasklist_project_id'),
			'userRates' => array(self::HAS_MANY, 'UserRate', 'user_rate_project_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'project_id' => 'Project',
			'project_code' => 'Project Code',
			'project_name' => 'Project Name',
			'project_description' => 'Project Description',
			'project_logo' => 'Project Logo',
			'project_status' => 'Project Status',
			'project_budget' => 'Project Budget',
			'project_category_id' => 'Project Category',
			'project_partner' => 'Project Partner',
			'project_programming_language_id' => 'Project Programming Language',
			'project_manager_id' => 'Project Manager',
			'project_labor' => 'Project Labor',
			'project_cost' => 'Project Cost',
			'project_currency' => 'Project Currency',
			'project_progress' => 'Project Progress',
			'project_rating' => 'Project Rating',
			'project_profit' => 'Project Profit',
			'project_created_date' => 'Project Created Date',
			'project_created_by_user_id' => 'Project Created By User',
			'project_start_date' => 'Project Start Date',
			'project_end_date' => 'Project End Date',
			'project_last_activity_date' => 'Project Last Activity Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('project_id',$this->project_id);
		$criteria->compare('project_code',$this->project_code,true);
		$criteria->compare('project_name',$this->project_name,true);
		$criteria->compare('project_description',$this->project_description,true);
		$criteria->compare('project_logo',$this->project_logo,true);
		$criteria->compare('project_status',$this->project_status,true);
		$criteria->compare('project_budget',$this->project_budget);
		$criteria->compare('project_category_id',$this->project_category_id);
		$criteria->compare('project_partner',$this->project_partner,true);
		$criteria->compare('project_programming_language_id',$this->project_programming_language_id);
		$criteria->compare('project_manager_id',$this->project_manager_id);
		$criteria->compare('project_labor',$this->project_labor);
		$criteria->compare('project_cost',$this->project_cost);
		$criteria->compare('project_currency',$this->project_currency,true);
		$criteria->compare('project_progress',$this->project_progress);
		$criteria->compare('project_rating',$this->project_rating,true);
		$criteria->compare('project_profit',$this->project_profit);
		$criteria->compare('project_created_date',$this->project_created_date,true);
		$criteria->compare('project_created_by_user_id',$this->project_created_by_user_id);
		$criteria->compare('project_start_date',$this->project_start_date,true);
		$criteria->compare('project_end_date',$this->project_end_date,true);
		$criteria->compare('project_last_activity_date',$this->project_last_activity_date,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	/**
	 * Lay ra ten cua nguoi quan ly project
	 * */
	
	public function getManagerName(){
		 $ProjectManager=User::model()->findByPk($this->project_manager_id);
		 return $ProjectManager->user_name; 
}
	/** Lay ra danh sach doi tuong User r thuoc project
	 * **/
	public function getMemberList(){
		$list = ProjectUser::model()->findAll('project_user_project_id',$this->project_id);
		foreach ($list as $item) {
			$memberlist= User::model()->findAll('user_id',$item);
			
		}
		//$memberList= User::model()->findAll('user_id',$list->)
		return $memberlist;
	}
	/**
	 * 
	 * Lay ra loai du an 
	 */
	public function getProjetCategoryName(){
	   $list = ProjectCategory::model()->findAll('project_catagory_id',$this->project_category_id);
	   return $list->project_catagory_name;
	   
	   
	}
	/**
	 * Lay ra ngay bat dau du an 
	 */
	public function getStartDate(){
		$dateTime=CDateTimeParser::parse($this->project_start_date,'yyyy-MM-dd hh:mm:ss');
		$date=CTimestamp::formatDate('m/d/Y',$dateTime);
		
	return $date;
	
	}
	/**
	 * Lay ra ngay ket thuc du an
	 */
	public function getEndDate(){
		 $dateTime=CDateTimeParser::parse($this->project_start_date,'yyyy-MM-dd hh:mm:ss');
		 return $date=CTimestamp::formatDate('m/d/Y',$dateTime);
	}
    /**
     * Phuong thuc tao gia tri cac loai tien
     * Enter description here ...
     */
	public function getListCurrency(){
		 return array(
		  'VND'=>'VND',
		  'USD'=>'USD',
		  'YEN'=>'YEN',
		  'EURO'=>'EURO',
		 );
    }	
	
    public static function listProject() {
    	$projects     = Project::model()->findAll(null,array('order'=>'project_end_date DESC'));
    	$result[NULL] = "---Chọn dự án---";
    	foreach ($projects as $project) {
    		$result[$project->project_id] = $project->project_name;
    	}
    	return $result;
    }
}
